//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.exam.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.LiveExam;
import com.ruoyi.live.base.domain.LiveExamTask;
import com.ruoyi.live.base.mapper.LiveExamMapper;
import com.ruoyi.live.base.mapper.LiveExamTaskMapper;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.commons.IConvert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LiveExamTaskService {
    private static final Logger log = LoggerFactory.getLogger(LiveExamTaskService.class);
    @Autowired
    LiveExamTaskMapper dao;
    @Autowired
    LiveExamMapper leDao;

    public LiveExamTaskService() {
    }

    public void addLiveExamTask(LiveExamTask entity) throws Exception {
        LiveExam exam = (LiveExam)this.leDao.selectById(entity.getExamId());
        if (exam.getState() != 0 && exam.getExamType() == 2) {
            log.error("比赛已经开始，无法添加任务");
            throw new Exception("比赛已经开始，无法添加任务");
        } else {
            try {
                entity.setCreateTime(System.currentTimeMillis());
                entity.setCreateUser(SessionInfo.getUserId());
                entity.setState(1);
                this.dao.insert(entity);
            } catch (Exception var4) {
                var4.printStackTrace();
                log.error("新增LiveExamTask失败,编号LiveExamTaskService-addLiveExamTask-1");
                throw new Exception("新增失败,编号LiveExamTaskService-addLiveExamTask-1");
            }
        }
    }

    public void updateLiveExamTask(LiveExamTask entity) throws Exception {
        LiveExam exam = (LiveExam)this.leDao.selectById(entity.getExamId());
        if (exam.getState() != 0) {
            log.error("比赛已经开始，无法修改任务");
            throw new Exception("比赛已经开始，无法修改任务");
        } else {
            try {
                this.dao.updateById(entity);
            } catch (Exception var4) {
                var4.printStackTrace();
                log.error("更新LiveExamTask失败,编号LiveExamTaskService-updateLiveExamTask-1,id:{}", entity.getId());
                throw new Exception("更新失败,编号LiveExamTaskService-updateLiveExamTask-1");
            }
        }
    }

    public LiveExamTask getLiveExamTask(Long id) {
        return (LiveExamTask)this.dao.selectById(id);
    }

    public int deleteLiveExamTask(String[] ids) throws Exception {
        if (ids.length == 0) {
            throw new Exception("请选择要删除的数据,编号LiveExamTaskService-deleteLiveExamTask-1");
        } else {
            LiveExamTask task = (LiveExamTask)this.dao.selectById(ids[0]);
            LiveExam exam = (LiveExam)this.leDao.selectById(task.getExamId());
            if (exam.getState() != 0) {
                log.error("比赛已经开始，无法删除任务");
                throw new Exception("比赛已经开始，无法删除任务");
            } else {
                try {
                    int count = 0;

                    for(int i = 0; i < ids.length; ++i) {
                        if (!ICommons.isNullOrEmpty(ids[i])) {
                            count += this.dao.deleteById(IConvert.string2long(ids[i]));
                        }
                    }

                    return count;
                } catch (Exception var6) {
                    var6.printStackTrace();
                    log.error("删除LiveExamTask失败,编号LiveExamTaskService-deleteLiveExamTask-2,ids:{}", ids.toString());
                    throw new Exception("删除的数据失败,编号LiveExamTaskService-deleteLiveExamTask-2");
                }
            }
        }
    }

    public List<LiveExamTask> query(LiveExamTask entity) {
        QueryWrapper<LiveExamTask> wrapper = (QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)(new QueryWrapper()).eq(!ICommons.isNullOrEmpty(entity.getId()), "id", entity.getId())).eq(!ICommons.isNullOrEmpty(entity.getState()), "state", entity.getState())).eq(!ICommons.isNullOrEmpty(entity.getCreateUser()), "create_user", entity.getCreateUser())).eq(!ICommons.isNullOrEmpty(entity.getCreateTime()), "create_time", entity.getCreateTime())).like(!ICommons.isNullOrEmpty(entity.getTaskName()), "task_name", entity.getTaskName())).eq(!ICommons.isNullOrEmpty(entity.getTaskType()), "task_type", entity.getTaskType())).eq(!ICommons.isNullOrEmpty(entity.getTaskProductCount()), "task_product_count", entity.getTaskProductCount())).eq(!ICommons.isNullOrEmpty(entity.getTaskDuration()), "task_duration", entity.getTaskDuration())).eq(!ICommons.isNullOrEmpty(entity.getExamId()), "exam_id", entity.getExamId())).eq(!ICommons.isNullOrEmpty(entity.getTaskCount()), "task_count", entity.getTaskCount())).eq(!ICommons.isNullOrEmpty(entity.getTaskOrder()), "task_order", entity.getTaskOrder())).eq(!ICommons.isNullOrEmpty(entity.getTaskProductType()), "task_product_type", entity.getTaskProductType())).eq(!ICommons.isNullOrEmpty(entity.getTaskProductId()), "task_product_id", entity.getTaskProductId());
        return this.dao.selectList(wrapper);
    }

    public void setProduct(String ids) throws Exception {
        List<LiveExamTask> list = this.dao.selectList(new QueryWrapper((new LiveExamTask()).setTaskType(1)));
        String[] idStr = ids.split(",");
        if (idStr[0].equals(idStr[1])) {
            throw new Exception("两种产品不能相同");
        } else {
            this.dao.updateById((new LiveExamTask()).setId(((LiveExamTask)list.get(0)).getId()).setTaskProductId(Long.valueOf(idStr[0])));
            this.dao.updateById((new LiveExamTask()).setId(((LiveExamTask)list.get(1)).getId()).setTaskProductId(Long.valueOf(idStr[1])));
        }
    }
}
